package leetcode.D100.T78;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class Solution {
    // 一刷
    /*public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> subset = new LinkedList<>();
        dfs(result, subset, nums, 0);
        return result;
    }
    private void dfs(List<List<Integer>> result, List<Integer> subset, int[] nums, int cur) {
        if(cur == nums.length) {
            result.add(new ArrayList<>(subset));
            return;
        }
        dfs(result, subset, nums, cur+1);
        subset.add(nums[cur]);
        dfs(result, subset, nums, cur+1);
        subset.remove(subset.size()-1);
    }*/

    // 二刷
    /*public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> results = new ArrayList<>();
        LinkedList<Integer> path = new LinkedList<>();
        dfs(results, path, nums, 0);
        return results;
    }

    private void dfs(List<List<Integer>> results, LinkedList<Integer> path, int[] nums, int cur) {
        if (cur == nums.length) {
            results.add((List<Integer>) path.clone());
            return;
        }
        dfs(results, path, nums, cur+1);
        path.add(nums[cur]);
        dfs(results, path, nums, cur+1);
        path.remove(path.size()-1);
    }*/

    // 三刷
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        dfs(result, path, nums, 0);
        return result;
    }

    void dfs(List<List<Integer>> result, List<Integer> path, int[] nums, int cur) {
        if (cur == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }
        dfs(result, path, nums, cur+1);
        path.add(nums[cur]);
        dfs(result, path, nums, cur+1);
        path.remove(path.size()-1);
    }
}
